<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="login_img">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <!-- 输入框区域 -->
      <el-form
        ref="form"
        label-width="0px"
        class="login_form"
        :model="form"
        :rules="formRules"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="form.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="success" :plain="true" @click="submitLogin"
            >登录</el-button
          >
          <el-button type="primary" @click="resetFieldsForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      // 表单用户名和密码双向绑定
      form: {
        username: "admin",
        password: "123456",
      },
      //表单验证
      formRules: {
        // 用户名验证
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        // 密码验证
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 用饿了么表单的方法resetFields对表单进行重置
    resetFieldsForm() {
      this.$refs.form.resetFields();
    },
    //点击登录
    submitLogin() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;
        else {
          //解构换别名
          let { data: res } = await this.$http({
            url: "login",
            method: "post",
            data: this.form,
          });
          // 如果返回对象中data.meta.status==200则表示登陆成功
          if (res.meta.status != 200) {
            this.$message.error("错了哦，您可能输错了账号密码!!!");
          } else {
            this.$message({ message: "欢迎回来！！！", type: "success" });
            // 将token存在sessionStorage中
            sessionStorage.setItem('TOKEN',res.data.token)
            this.$router.push("/home");
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 500px;
  height: 400px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border: 1px solid rgb(10, 175, 38);

  .login_img {
    width: 100px;
    height: 100px;
    background-color: #eee;
    border: 1px solid rgb(10, 175, 38);
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50px;

    img {
      height: 100%;
      width: 100%;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 20%;
  width: 100%;
  padding: 0 30px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: center;
}
</style>
